﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using WLMCoreLibrary;
using MiWebSite_Core;
using System.Xml;
using System.Xml.XPath;

namespace MWS_Main.login
{
    public partial class _default : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            WebSiteMainConfiguration config = WebSiteMainConfiguration.GetConfig();
            List<ParametroTransformacionXML> parametros = new List<ParametroTransformacionXML>();
            parametros.Add(new ParametroTransformacionXML("skinPath", config.Settings["skinPath"].value));
            parametros.Add(new ParametroTransformacionXML("isMobile", MiWebSiteFunctions.isMobileBrowser().ToString().ToLower()));
            //parametros.Add(new ParametroTransformacionXML("isHome", "true"));
            if (Request.QueryString["xml"] != null)
            {
                dbConnector ConexionDB = new dbConnector(config.Connections[0].value);
                MiWebSitePage pagina = new MiWebSitePage(ref ConexionDB);

                XmlDocument documento = new XmlDocument();
                XmlNode XMLModuloExtraido = documento.CreateElement("Contenido");
                pagina.Contenido = documento.CreateElement("MiWebSiteContent");
                XMLModuloExtraido.InnerText = "<form name='form1' method='post'><div><h1>Ingrese sus Datos de Usuario</h1>" +
                    "Usuario o Email:<br /><input name='userName' type='text' id='userNAme' class='rounded inputLogin' />" +
                    "<br />password:<br /><input name='password' type='password' id='password' class='rounded inputLogin' />" +
                    "<br /><input type='submit' name='loginButton' value='Login' id='loginButton' class='rounded' /><input type='button' name='refreshPassButton' value='Recuperar Mi Password' id='refreshPassButton' class='rounded' /></div></form>";
                
                pagina.Contenido.AppendChild(XMLModuloExtraido);

                string xmlportada = WLMFunctions.SerializeObject(pagina, true);
                ConexionDB.CloseConnection();
                Response.ContentType = "text/xml";
                Response.Write(xmlportada);
                Response.End();
            }
            else
            {
                string HTMLPagina = WLMFunctions.TransformXML(config.Settings["siteURL"].value + "login?xml=true", config.Settings["siteURL"].value + config.Settings["skinPath"].value + "index.xsl", parametros);
                Response.Write(HTMLPagina);
                if (Request["logout"] != null)
                {
                    CurrentUser.LogOutUser();
                    Response.Redirect("/");
                }
                if (Request["username"] != null)
                {
                    LogearUsuario(Request["username"], Request["password"], Convert.ToBoolean(Request["loginRecordar"]));
                }
                Response.End();
            }
        }

        protected void LogearUsuario(string strusername, string strpassword, Boolean recordarUsuario)
        {
            WebSiteMainConfiguration config = WebSiteMainConfiguration.GetConfig();

            dbConnector ConexionDB = new dbConnector(config.Connections[0].value);
            MiWebSiteUser usuario = new MiWebSiteUser(WLMFunctions.CleanQuery(strusername), ref ConexionDB);
            if (usuario.password == WLMFunctions.CleanQuery(strpassword))
            {
                if (recordarUsuario)
                    CurrentUser.LogOnUser(usuario, recordarUsuario);
                else
                    CurrentUser.LogOnUser(usuario);
                ConexionDB.CloseConnection();

                Response.Redirect("/");
            }
            Response.Redirect("/login/?error");
        }


    }
}